﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cn.Edwon.Web.Session;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web.Security;

namespace Cn.Edwon.Web.Ac {
    public class AcHelper {
        public static Common.Logging.ILog log = Common.Logging.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static String BuildRemoteKey(String rUserKey,String rPwd) {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(rUserKey + rPwd + DateTime.Now.ToString("dhm")+"19780301","md5");
        }
        public static AcState Excute(ISessActor rActor, String rUrl) {
            String _url = rUrl.ToLower();
            if (ClientHelper.IsPublicUrl(_url)) {
                log.Debug("AC:Public url,passed");
                return AcState.Normal;
            }
            if (rActor!=null) {
                //如果是登录后导向的页面，只校验是否已经登录
                if (!String.IsNullOrEmpty(ClientHelper.Ac.RedirectAfterLogin)
                    && _url.IndexOf((ClientHelper.RootUrl + ClientHelper.Ac.RedirectAfterLogin).ToLower()) != -1) {
                    log.Debug("AC: RedirectAfterLogin,passed");
                    return AcState.Normal;
                }

                //检查是否符合只要登录就能够访问的页面
                if (ClientHelper.IsActivedUrl(_url)) {
                    log.Debug("AC: Actived url,passed");
                    return AcState.Normal;
                }

                foreach (String u in rActor.SecurityUrls) {
                    if (Regex.IsMatch(_url, u, RegexOptions.IgnoreCase)) {
                        log.Debug("AC: Pattern Matched,passed");
                        return AcState.Normal;
                    }
                }

                log.Debug("AC: Power Low,Unpassed");
                return AcState.PowerLow;
            } else {
                log.Debug("AC: Session lost,Unpassed");
                return AcState.SessionLost;
            }
        }
    }
}
